<?php

namespace app\service;

//玩家材料服务层

use app\model\Card;
use app\model\FactoryIngredient;
use app\model\UserCard;
use app\model\UserIngredient;

class UserIngredientsService extends BaseService
{

    //添加玩家材料
    public function add_user_ingredients($user_id, $ingredient_id, $num): array
    {
        //查询材料是否存在
        $ingredient = FactoryIngredient::where("id", $ingredient_id)->find();
        if (!$ingredient) {
            return $this->returnFail("材料不存在");
        }

        //查询玩家是否已经拥有该材料
        $user_ingredient = UserIngredient::where("user_id", $user_id)->where("ingredients_id", $ingredient_id)->find();
        if (!$user_ingredient) {
            $user_ingredient = new UserIngredient();
            $user_ingredient->user_id = $user_id;
            $user_ingredient->ingredients_id = $ingredient_id;
        }
        $user_ingredient->num = $user_ingredient->num + $num;
        $user_ingredient->save();

        return $this->returnSuccess("添加成功");
    }

}